package com.moliku.insurance.process.third;

import com.alibaba.fastjson.JSONObject;
import com.moliku.insurance.common.util.JacksonUtils;
import com.moliku.insurance.model.dto.InsuranceBaseParam;
import com.moliku.insurance.model.vo.InsuranceBaseVo;
import com.moliku.insurance.model.vo.huaan.HuaAnInsuranceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 平安第三方调用
 * @author way
 * @date 2021-03-06
 */
@Slf4j
@Component
public class HuaAnThirdApi extends BaseThirdApi{

    /**
     *带token调用
     *@param baseUrl 基础路径
     *@param    t 请求
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    @Override
    public <R> HuaAnInsuranceVo<R> execute(String baseUrl, InsuranceBaseParam<R> t){
        String url=baseUrl+t.url();
        Map<String, Object> params= JacksonUtils.objToMap(t);
        String ret= InsuranceBaseParam.METHOD.GET.equals(t.methodType())?get(url,params):post(url,params);
        log.error("远程调用 。请求url:{},请求参数:{} .返回结果:{}",url, JSONObject.toJSON(params),ret);
        return toResponse(ret,t.responseType());
    }


    /**
     *消息转换为Response
     *@param responseStr 返回数据
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    @Override
    public <T> HuaAnInsuranceVo<T> toResponse(String responseStr, Class<T> clazz) {
        HuaAnInsuranceVo response= JacksonUtils.jsonToObj(responseStr,HuaAnInsuranceVo.class);
        response.setData(JacksonUtils.jsonToObj(JacksonUtils.objToJson(response.getData()),clazz));
        if(null==response){
            log.error("远程调用格式化返回为空.responseStr:{}",responseStr);
            return null;
        }
        return response;
    }

}
